package com.mjh.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mjh.exception.ClientParameterException;
import com.mjh.mapper.WtzxMapper;
import com.mjh.pojo.Wtzx;
import com.mjh.pojo.vo.WtzxVo;
import com.mjh.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 *
 * 问题咨询 前端控制器
 *
 *
 * @author MJH
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/inquiry")
public class WtzxController {


    @Autowired
    private HttpServletRequest request;

    @Autowired
    private WtzxMapper wtzxMapper;

    /**
     * 查询所有问题咨询
     * @param currentPage  当前页
     * @param pageSize  每页数量
     * @param isPersonal  是否为个人问题咨询
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity<Object> queryInquiryAll(@RequestParam(defaultValue = "1") Integer currentPage,
                                                 @RequestParam(defaultValue = "10")  Integer pageSize,
                                                  boolean isPersonal){

        Page<WtzxVo> page = new Page<>(currentPage,pageSize);
        Integer userId=null;
        if (isPersonal){
            userId = (Integer)JwtUtil.getPayload(request, "userId");
        }


        Page<WtzxVo> wtzxPage = wtzxMapper.queryInquiryAll(page,userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",wtzxPage.getTotal());
        map.put("currentPage",wtzxPage.getCurrent());
        map.put("list",wtzxPage.getRecords());

        return ResponseEntity.ok(map);
    }

    /**
     * 新增提问内容
     * @param center 咨询内容
     */
    @PostMapping("/add")
    public void  insertInquiry(@RequestBody String center){

        if (center==null) throw new ClientParameterException("提问内容不能为空");
        String zx =(String) JSONUtil.parseObj(center).get("center");

        Wtzx wtzx = new Wtzx();
        wtzx.setWCreateUser((Integer) JwtUtil.getPayload(request,"userId"));
        wtzx.setWTwnr(zx);
        wtzxMapper.insert(wtzx);

    }

    /**
     * 根据编号查询
     * @param id  问题咨询
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<Object> queryByID(@PathVariable("id") Integer id){
        Wtzx wtzx = wtzxMapper.selectById(id);
        return ResponseEntity.ok(wtzx);
    }




}
